<template>
  <div class="changePassword">
    <van-nav-bar
      title="修改密码"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="information.userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        maxlength="11"
        v-model="information.phone"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="information.password"
        type="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >确认</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { saveOrUpdateUser } from "../common/Api/index.js";
import { Toast } from "vant";
export default {
  data() {
    return {
      information: {
        userName: "",
        password: "",
        phone: "",
      },
    };
  },
  mounted() {
    this.information = JSON.parse(sessionStorage.getItem("information"));
    console.log(this.information);
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      let arr = {
        userName: this.information.userName,
        password: this.information.password,
        phone: this.information.phone,
        account: this.information.account,
        uId: this.information.uId,
      };
      saveOrUpdateUser(arr).then((res) => {
        if (res == "修改成功") {
          sessionStorage.removeItem("information");
          sessionStorage.setItem("active", JSON.stringify("setting"));
          Toast.success("修改成功");
          this.$router.push("/");
        } else {
          Toast.fail(res);
        }
        console.log(res);
      });
    },
  },
};
</script>